﻿using AutoMapper;
using Menus.DataBase;
using Menus.Dtos;
using Menus.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Menus.Services
{
    public class UserMenusRepository : IUserMenusRepository
    {
        private MenuDbContext _menuDbContext;
        public UserMenusRepository(MenuDbContext context)
        {
            _menuDbContext = context;
        }

        public int GetPerMissionByToken(string role)
        {
            switch (role)
            {
                case "admin":
                    {
                        return 0;
                    }
                case "user":
                    {
                        return 1;
                    }
                default:
                    {
                        return -1;
                    }
            }
        }

        public async Task<IEnumerable<UserMenu>> GetUserMenusAsync(int permisson)
        {
            if (permisson == -1)
            {
                return null;
            }
            return await _menuDbContext.UserMenus.Include(t => t.userSecondMenus).Where(t => ((int)t.permission) >= permisson).ToListAsync();
        }
       
        public Task<List<UserSecondMenu>> MenuSecondMenusChange0btainMenusPermissionDtoAsync(IEnumerable<UserMenu> userMenus)
        {
           
            return Task.Run(() => {
                List<UserSecondMenu> userSecondMenus = new List<UserSecondMenu>();
                foreach (UserMenu userMenu in userMenus)
                {
                    userSecondMenus.AddRange(userMenu.userSecondMenus);
                };
                return userSecondMenus;
            });
        }
    }
}
